﻿/**
 * Draggable
 *
 * @author		Allen Chou
 * @version		1.0.1 (last update: Feb 14 2009)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.display {
	import flash.display.*;
	import flash.events.*;

	/**
	 * The <code>Draggable</code> class can be used as a symbol base class,
	 * whose instances have the default behavior of being able to be dragged by the mouse cursor.
	 */
	public class Draggable extends MovieClip {
		
		private var _active:Boolean = true;
		
		public function Draggable() {
			active = true;
			addEventListener(Event.ADDED_TO_STAGE, added);
			addEventListener(Event.REMOVED_FROM_STAGE, removed);
		}
		
		/**
		 * Whether the <code>Draggable</code> object is able to be dragged by the mouse cursor.
		 */
		public function get active():Boolean { return _active; }
		public function set active(value:Boolean):void {
			_active = value;
			if (_active) {
				addEventListener(MouseEvent.MOUSE_DOWN, begin);
			} else {
				removeEventListener(MouseEvent.MOUSE_DOWN, begin);
			}
		}
		
		private function begin(e:Event):void {
			this.startDrag();
		}
		
		private function finish(e:Event):void {
			this.stopDrag();
		}
		
		private function added(e:Event):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, finish);
		}
		
		private function removed(e:Event):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, finish);
		}
		
	}
}